package com.sch.wanandroid.ui.main.tab

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sch.wanandroid.constants.Constants
import com.sch.wanandroid.entity.TabTypeBean
import com.sch.wanandroid.network.NetWorkManager
import kotlinx.coroutines.launch
import java.lang.Exception

/**
 * Created by Sch.
 * Date: 2020/12/14
 * description:
 */
class TabViewModel : ViewModel() {
    private val _tabList = MutableLiveData<List<TabTypeBean>>()
    val tabListLiveData: LiveData<List<TabTypeBean>> = _tabList

    private val _errorMsg = MutableLiveData<String?>()
    val errorMsg: LiveData<String?> = _errorMsg

    fun getTabListData(type: Int) {
        viewModelScope.launch {
            try {
                if (type == Constants.PROJECT_TYPE) {
                    val result = NetWorkManager.apiService.getProjectTreeData()
                    if (result.errorCode != 0) {
                        _errorMsg.value = result.errorMsg
                    } else {
                        _tabList.value = result.data
                    }
                } else {
                    val result = NetWorkManager.apiService.getWxArticleChaptersData()
                    if (result.errorCode != 0) {
                        _errorMsg.value = result.errorMsg
                    } else {
                        _tabList.value = result.data
                    }
                }
            } catch (e: Exception) {
                _errorMsg.value = e.toString()
            }
        }
    }
}